package com.bawei.yuekao.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.RemoteViews;

import com.bawei.yuekao.R;
import com.bawei.yuekao.entity.Song;

import java.io.IOException;
import java.util.List;

import androidx.annotation.Nullable;

public class MusicService  extends Service {



    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicBinder();
    }
    public class MusicBinder extends Binder {
        public static final int MODE_ORDER = 0;//顺序
        public static final int MODE_RANDOM = 1;//随机
        public static final int MODE_DANQU = 2;//单曲
        private MediaPlayer mediaPlayer = new MediaPlayer();

        private List<Song> list;
        private int position;
        private int mode = MODE_ORDER;//播放模式

        //切歌
        public void play(int position){
            this.position = position;//更换下标
            if(mediaPlayer != null){
                mediaPlayer.reset();//!!!!!!!!重置资源
                try {
                    mediaPlayer.setDataSource(list.get(position).getData());
                    mediaPlayer.prepareAsync();
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {//!!!!!一定要准备完成
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mediaPlayer.start();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //！！！！发送通知 更新通知文字和图片
                showNotification();
                //更新播放栏+弹出窗体  发送广播
                Intent intent = new Intent();
                intent.setAction("com.music.ui");
                sendBroadcast(intent);





            }

        }

        public void setList(List<Song> list){
            this.list = list;

        }
        //播放
        public void start(){
            if(mediaPlayer != null){
                mediaPlayer.start();
            }
        }

        //暂停
        public void pause(){
            if(mediaPlayer != null){
                mediaPlayer.pause();
            }
        }
        //播放和暂停
        public void pauseOrstart(){
            if(mediaPlayer != null){
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                }else{
                    mediaPlayer.start();
                }
            }
        }

        //返回音乐总时长
        public int getDuration(){
            if(mediaPlayer != null){
                return mediaPlayer.getDuration();
            }
            return 0;
        }

        //返回音乐当前进度
        public int getCurrent(){
            if(mediaPlayer != null){
                return mediaPlayer.getCurrentPosition();
            }
            return 0;
        }
        //拖拽进度条
        public void seekTo(int time){
            if(mediaPlayer != null){
                mediaPlayer.seekTo(time);
            }
        }
        //下一首
        public void next(){
           if(mode == MODE_ORDER){//顺序
               if(position == list.size()-1){
                   position = 0;
               }else{
                   position++;
               }

           }else if(mode == MODE_RANDOM){//随机
              position = (int) (Math.random()*list.size());
           }
           play(position);
        }

        //上一首
        public void pre(){
            if(mode == MODE_ORDER){//顺序
                if(position == 0){
                    position = list.size()-1;
                }else{
                    position--;
                }

            }else if(mode == MODE_RANDOM){//随机
                position = (int) (Math.random()*list.size());
            }
            play(position);
        }
        //返回当前歌曲名称
        public String getName(){
            if(mediaPlayer != null){
                return  list.get(position).getName();
            }
            return "";
        }

        //返回当前歌曲图片
        public String getAlbum(){
            if(mediaPlayer != null){
                return  list.get(position).getAlbum();
            }
            return "";
        }

        //返回当前歌手
        public String getsinger(){
            if(mediaPlayer != null){
                return  list.get(position).getSinger();
            }
            return "";
        }

        //返回当前歌曲低至
        public String getData(){
            if(mediaPlayer != null){
                return  list.get(position).getData();
            }
            return "";
        }

        //切换播放模式
        public void changemode(int mode){
            this.mode = mode;
        }

        //显示通知
        public void showNotification(){
            Notification.Builder builder = new Notification.Builder(getApplicationContext());
            builder.setSmallIcon(R.mipmap.ic_launcher_round);
            //自定义布局
            RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.no);
            Bitmap bitmap = BitmapFactory.decodeFile(getAlbum());
            remoteViews.setImageViewBitmap(R.id.no_iv,bitmap);
            remoteViews.setTextViewText(R.id.no_tv_name,getName());
            //监听+广播

            {
                Intent intent = new Intent();
                intent.setAction("com.music.pre");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.no_iv_pre, pendingIntent);
            }
            {
                Intent intent = new Intent();
                intent.setAction("com.music.start");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.no_iv_start, pendingIntent);
            }
            {
                Intent intent = new Intent();
                intent.setAction("com.music.pause");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.no_iv_pause, pendingIntent);
            }
            {
                Intent intent = new Intent();
                intent.setAction("com.music.next");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.no_iv_next, pendingIntent);
            }
            builder.setCustomContentView(remoteViews);
            Notification notification = builder.build();

            startForeground(1,notification);
        }

    }
}
